package com.insurance.service.impl;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.mapper.InsuranceMapper;
import com.insurance.pojo.Insurance_bd;
import com.insurance.service.InsuranceService;
import com.insurance.utils.ConversionDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User1: 李辽
 * Date: 2020/10/27 19:06
 * Description:
 * Version: V1.0
 */
@Service
public class InsuranceServiceImpl implements InsuranceService {

    @Autowired(required = false)
    private InsuranceMapper insuranceMapper;


    @Override
    public List<Map> queryInsuredUser(String user_phone) {
        List<Map> mapList = insuranceMapper.queryInsuredUser(user_phone);
        //将数据为datetime类型的数据转换为String类型
        for (Map map:mapList ) {//将交强险开始时间转换为String
            if (map.get("high_stime") != null){
                String high_stime = ConversionDateUtils.conversionDateStringUtils(map.get("high_stime"));
                map.put("high_stime",high_stime);
            }else {
                map.put("high_stime","用户暂未添加");
            }
        }
        for (Map map:mapList) {//将交强险结束时间转换为String
            if (map.get("high_ltime") != null){
                String high_ltime = ConversionDateUtils.conversionDateStringUtils(map.get("high_ltime"));
                map.put("high_ltime",high_ltime);
            }else {
                map.put("high_ltime","用户暂未添加");
            }
        }
        for (Map map:mapList) {//将商业保单起始时间转换为Sring类型
            if (map.get("business_ftime") != null){
                String business_ftime = ConversionDateUtils.conversionDateStringUtils(map.get("business_ftime"));
                map.put("business_ftime",business_ftime);
            }else {
                map.put("business_ftime","用户暂未添加");
            }
        }
        for (Map map:mapList) {//将商业保单结束时间转换为Sring类型
            if (map.get("business_ltime") != null){
                String business_ltime = ConversionDateUtils.conversionDateStringUtils(map.get("business_ltime"));
                map.put("business_ltime",business_ltime);
            }else {
                map.put("business_ltime","用户暂未添加");
            }
        }

        return mapList;
    }



    @Override
    public List<Map> queryCardByNameOrPhone(String user_name, String user_phone) {

        return insuranceMapper.queryCardByNameOrPhone(user_name,user_phone);
    }

    @Override
    public ResultData addProtect(Map map) {
        System.out.println("进入service层内容："+map.toString());
        ResultData resultData = new ResultData();
        Insurance_bd insurance_bd = new Insurance_bd();

        //通过当前车牌号查询车主卡id
        Map map1 = insuranceMapper.queryOwnerId(map.get("insure_amount"));
        int owner_card_id = (int) map1.get("owner_card_id");
        System.out.println("=========查询到的车主卡id为："+owner_card_id);

        String high_stime = (String) map.get("high_stime");
        String high_ltime = (String) map.get("high_ltime");
        String business_ftime = (String) map.get("business_ftime");
        String business_ltime = (String) map.get("business_ltime");
        //将表单表中的信息存入实体插入。返回新插入的id
        try {
            insurance_bd.setInsurance_com_id(Integer.parseInt(map.get("insurance_com_id").toString()));
            insurance_bd.setHigh_insurance((String) map.get("high_insurance"));
            insurance_bd.setBusiness_insurance((String) map.get("business_insurance"));
            Date high_stime2 = ConversionDateUtils.conversionDateUtils(high_stime);
            System.out.println("===========将交强险开始时间转换为Date类型格式为："+high_stime2);
            insurance_bd.setHigh_stime(ConversionDateUtils.conversionDateUtils(high_stime));
            insurance_bd.setHigh_ltime(ConversionDateUtils.conversionDateUtils(high_ltime));
            insurance_bd.setBusiness_ftime(ConversionDateUtils.conversionDateUtils(business_ftime));
            insurance_bd.setBusiness_ltime(ConversionDateUtils.conversionDateUtils(business_ltime));
            insurance_bd.setOwner_card_id(owner_card_id);
            insurance_bd.setInsure_man((String) map.get("user_name"));
        }catch (NullPointerException e){
            e.printStackTrace();
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("请输入完整信息");
        }

        System.out.println("=============service中实体数据："+insurance_bd);

        int result = insuranceMapper.addProtect(insurance_bd);//添加保单信息
        int insurance_id = insurance_bd.getInsurance_id();//获取最新插入的保单id
        System.out.println("===========最新添加的保单id为："+insurance_id);

        int laseResult = 0;
        if (result > 0){
            List<Map> arr = (List<Map>) map.get("baodanXinxi");//获取用户选择的保险信息
            System.out.println("===========从map中取出的数组内容为："+arr.toString());
            for (Map baoxian:arr) {
                map.put("insurance_id",insurance_id);//将保单id加入map中
                Insure1 insure1 = new Insure1();//创建保险实体
                int insure_name_id = (int) baoxian.get("insure_name_id");
                Double insure_premium = Double.valueOf(baoxian.get("baofei").toString());
                Double insure_amount = Double.valueOf(baoxian.get("baoxiane").toString());
                insure1.setInsure_name_id(insure_name_id);
                insure1.setInsure_premium(insure_premium);
                insure1.setInsure_amount(insure_amount);
                insure1.setInsure_inculding_no_com((Integer) baoxian.get("checked"));
                System.out.println("=========保险实体内容为："+insure1);

                laseResult = insuranceMapper.addBaoXianXinXi(insure1);//将保险信息全部输入数据库
                int insure_id = insure1.getInsure_id();//获取刚刚插入的保险信息id
                if(insure_id > 0){
                    int result1 = insuranceMapper.addZhongJianBiao(insure_id,insurance_id);
                }
            }
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("保单添加成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务器错误");
        }


        return resultData;
    }

    @Override
    public int editInsurance(Map map) {//更改保单信息接口
        int result = insuranceMapper.editUserName(map);//更改保单中的被保险人
        int resultDate = 0;
        if (result > 0 ){
            resultDate = insuranceMapper.editPhoneAndName(map);//更改被保险人电话号码
        }
        return 1;
    }

    @Override
    public List<Map> queryAllInformation(int user_id, int insurance_id) {
        //查询被保险人的所有信息
        Map mapList1 = insuranceMapper.queryAllInformation(user_id, insurance_id);
        String business_ltime = ConversionDateUtils.conversionDateStringUtils(mapList1.get("business_ltime"));
        mapList1.put("business_ltime",business_ltime);
        String business_ftime = ConversionDateUtils.conversionDateStringUtils(mapList1.get("business_ftime"));
        mapList1.put("business_ftime",business_ftime);
        String high_stime = ConversionDateUtils.conversionDateStringUtils(mapList1.get("high_stime"));
        mapList1.put("high_stime",high_stime);
        String high_ltime = ConversionDateUtils.conversionDateStringUtils(mapList1.get("high_ltime"));
        mapList1.put("high_ltime",business_ftime);

        List<Map> mapList = null;
        if (mapList1 != null){//判断当前用户id是否有对应的保单id信息
            mapList = insuranceMapper.queryAllZJB(insurance_id,user_id);//查询当前保单所有的保险信息
            mapList.add(0,mapList1);
        }
        return mapList;
    }
}





















